<template>
  <el-table :data="filterTableData" style="width: 100%" size="default">
    <el-table-column label="序号" prop="disid" />
    <el-table-column label="身份证号" prop="patientid" />
    <el-table-column label="姓名" prop="patientname" />
    <el-table-column label="医生" prop="doctorname" />
    <el-table-column label="金额" prop="dismoney" />
    <el-table-column label="操作" width="200">
      <template #header>
        <el-button type="primary" @click="dialogVisible = true">查询</el-button>
      </template>
      <template #default="scope">
        <!-- <el-button
          size="default"
          type="primary"
          @click="handleViewPrescription(scope.row)"
        >
          查看药方
        </el-button> -->

        <el-button
          size="default"
          :type="scope.row.ispay === 1 ? 'success' : 'primary'"
          :disabled="scope.row.ispay === 1"
          @click="handlePay(scope.row)"
        >
          {{ scope.row.ispay === 1 ? '已缴费' : '缴费' }}
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  <!-- 查询信息 -->
  <el-dialog title="查询信息" v-model="dialogVisible" width="350px">
    <el-form v-model="form" label-width="80px">
      <el-form-item label="姓名">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="身份证">
        <el-input v-model="form.id"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSubmit">查询</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
  <!-- 查看药方 -->
  <!-- <el-dialog title="药方信息" v-model="prescriptionDialogVisible" width="350px">
    <div v-if="currentPrescription">
      <p>药方: {{ currentPrescription.medicines }}</p>
    </div>
  </el-dialog> -->

  <!-- 查询 -->
  <div style="position: fixed; bottom: 160px; right: 60px">
    <el-button @click="handleSearch" size="large" circle>
      <el-icon style="font-size: 24px">
        <Search />
      </el-icon>
    </el-button>
  </div>
  <!-- 支付码 -->
  <div v-if="payCodeVisible" class="pay-code-container">
    <img src="../../static/123.png" alt="支付码" />
  </div>
</template>

<script lang="ts" setup>
import { computed, reactive, ref } from 'vue'
import axios from 'axios'
import { ElMessage, ElMessageBox } from 'element-plus'
import service from '../request/index'
// import { onMounted } from 'vue'

const dialogVisible = ref(true)
const form = reactive({
  name: '',
  id: '',
})
const tableData = ref([])
// 搜索框
const search = ref('')

// 通过医生搜索
const filterTableData = computed(() => {
  if (!tableData.value) {
    return []
  }
  return tableData.value.filter(
    (data) =>
      !search.value ||
      data.doctorname.toLowerCase().includes(search.value.toLowerCase()),
  )
})

const handleSearch = () => {
  dialogVisible.value = true
}

const handleSubmit = () => {
  // 构造请求体数据
  const postData = {
    patientId: form.id,
    patientName: form.name,
  }
  service
    .post('/Diagnosis/patientDiagnosis', postData)
    .then((response) => {
      console.log(response)
      tableData.value = response.data
      ElMessage.success('查询成功')
    })
    .catch((error) => {
      console.error(error)
      ElMessage.error('查询失败')
    })
    .finally(() => {
      dialogVisible.value = false
    })
}
const payCodeImageUrl = ref('')
const handlePay = (row) => {
  // 构造请求体数据，假设需要发送的数据包含在 row 中
  const payData = {
    id: form.id,
  }
  const getBase64 = (data) => {
    const blob = new Blob([data], { type: 'png' }) //类型一定要写！！！
    return new Promise((resolve, reject) => {
      const reader = new FileReader()
      reader.readAsDataURL(blob)
      reader.onload = () => resolve(reader.result)
      reader.onerror = (error) => reject(error)
    })
  }
  ElMessageBox.confirm('确定要为该用户缴费吗？', '缴费确认', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(() => {
      // 发起缴费请求，假设API端点是 /setIspayByID
      service
        .post('/setIspayByID', payData)
        .then(() => {
          service
            .post('/createCode', {
              content: 'Payment successful',
              name: '123',
            })
            .then((response) => {
              // payCodeImageUrl.value = response.data.imageUrl;
              // payCodeVisible.value = true;
              ElMessage.success('生成二维码成功')
            })
            .catch((error) => {
              console.error(error)
              ElMessage.error('生成获取码失败')
            })
          axios.get('http://127.0.0.1:8082/getCode').then(async (res) => {
            // payCodeImageUrl.value = await getBase64(res.data)
            // console.log(payCodeImageUrl.value)
            payCodeVisible.value = true
            ElMessage.success('获取二维码成功')
          })
        })
        .catch((error) => {
          console.error(error)
          ElMessage.error('缴费失败')
        })
    })
    .catch(() => {
      ElMessage.info('已取消操作')
    })
}

// 查看药方
const prescriptionDialogVisible = ref(false)
const currentPrescription = ref(null)
const handleViewPrescription = (row) => {
  // 假设每行数据中包含药方信息的字段名为 'med1'
  currentPrescription.value = row
  prescriptionDialogVisible.value = true
}

// 显示二维码
const payCodeVisible = ref(false)
</script>
<style scoped>
.pay-code-container {
  /* 样式根据需要自定义 */
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #f9f9f9;
  border: 1px solid #dcdcdc;
  padding: 10px;
  border-radius: 5px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  z-index: 1000;
}

.pay-code-container img {
  width: 300px;
  height: auto;
  display: block;
  margin: 0 auto;
}
</style>